package com.situ.company.employee.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.IEmployeeService;
import com.situ.company.employee.service.impl.EmployeeServiceImpl;
import com.situ.company.util.FmtRequest;
import com.situ.company.util.MD5;

@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IEmployeeService service = new EmployeeServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=UTF-8");
		Object res = null;
		switch (req.getParameter("action")) {
		case "resetPass":
			res = resetPass(req);
			break;
		case "updPass":
			res = updPass(req);
			break;
		case "delPic":
			res = delPic(req);
			break;
		case "setDept":
			res = setDept(req);
			break;
		case "upd":
			res = upd(req);
			break;
		case "get":
			res = get(req);
			break;

		case "add":
			res = add(req);
			break;
		case "del":
			res = del(req);
			break;
		case "page":
			res = page(req);
			break;
		case "reg":
			res = reg(req);
			break;
		case "login":
			res = login(req);
			break;
		case "logout":
			req.getSession().removeAttribute("user");
			resp.sendRedirect(req.getContextPath() + "/web/login.jsp");
			return;
		default:
			break;
		}
//		PrintWriter writer = resp.getWriter();
//		writer.write(reg);
//		writer.flush();
//		writer.close();
//		writer = null;
		FmtRequest.write(resp.getWriter(), res);
	}

	private String resetPass(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		model.setCode(req.getParameter("code"));
		model.setPass(MD5.encode("123456"));
		return service.updateActive(model);
	}

	private String updPass(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		// System.out.println(req.getParameter("pass"));
		model.setCode(req.getParameter("code"));

		model.setPass(MD5.encode(req.getParameter("pass")));
		return service.updateActive(model);
	}

	private String delPic(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		model.setCode(req.getParameter("code"));
		return service.delPic(model);
	}

	private String setDept(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		model.setCode(req.getParameter("code"));
		model.setCodeDept(req.getParameter("codeDept"));
		return service.setDept(model);
	}

//	private String updPass(HttpServletRequest req) {
//		return service.updPass();
//	}
//
//	private String resetPass(HttpServletRequest req) {
//		return service.resetPass();
//	}

//	private  resetPass(HttpServletRequest req) {
//		// TODO Auto-generated method stub
//		return null;
//	}

	private String upd(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		model.setCode(req.getParameter("code"));
		model.setName(req.getParameter("name"));
		model.setCodeDept(req.getParameter("codedept"));
		return service.update(model);
	}

	private Object get(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		model.setCode(req.getParameter("code"));
		return service.selectModel(model);
	}

	private String del(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		model.setCode(req.getParameter("code"));
		return service.delete(model);
	}

	private String add(HttpServletRequest req) {
		EmployeeModel model = new EmployeeModel();
		model.setCode(req.getParameter("code"));
		model.setName(req.getParameter("name"));
		model.setCodeDept(req.getParameter("codedept"));
		return service.insert(model);
	}

	private Object page(HttpServletRequest req) {
		String code = req.getParameter("code");
		String name = req.getParameter("name");
		String pageIndex = req.getParameter("pageIndex");
		String pageLimit = req.getParameter("pageLimit");
		EmployeeModel model = new EmployeeModel();
		model.setCode(code);
		model.setName(name);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<EmployeeModel> list = service.selectList(model);
		// 带有查询条件和分页条件，得到的当前页面要显示的数据记录
		// 带有查询条件，不带有分页条件，得到的记录条数
		Integer count = service.selectCount(model);
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("count", count);
		return map;
	}

	private String login(HttpServletRequest req) {
		EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class);
		String res = service.login(model);
		if ("1".equals(res)) {
			req.getSession().setAttribute("user", service.selectModel(model));
		}
		return res;
	}

	private String reg(HttpServletRequest req) {
//		String code = req.getParameter("code");
//		String pass = req.getParameter("pass");
//		String name = req.getParameter("name");
//		EmployeeModel model = new EmployeeModel(code,name,pass);
		EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class);
		return service.insert(model);
	}

}
